/*
 * drv_uart.c
 *
 *  Created on: Oct 14, 2013
 *      Author: Omar Sevilla
 */


#include "derivative.h"
#include "stdtypedef.h"
#include "drv_uart.h"

void init_uart(){
	T_UWORD sbr = 0,temp = 0;
	//Set clock to UART Module
	SIM_SOPT2 |= (1 << 26);		//Clock set
	SIM_SOPT2 |= (1 << 16);		//PLL source selected
	
	//Enable clock
	SIM_SCGC4 |= (1 << 10);		//Enable Clock for UART0 Module
	SIM_SCGC4 |= (1 << 11);     //Enable Clock for UART1 Module
	
	//Set port clock 
	SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK;
	SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK;
	
	
	PORTA_PCR1 |= PORT_PCR_MUX(2);
	PORTA_PCR2 |= PORT_PCR_MUX(2);
	
	PORTE_PCR0 |= PORT_PCR_MUX(3);
	PORTE_PCR1 |= PORT_PCR_MUX(3);
	
	/*Disable Transmitter and Receiver*/
	 UART0_C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK );
	 UART1_C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK );
	 

	 /* Configure the UART for 8-bit mode, no parity */
	 UART0_C1 = 0;	/* We need all default settings, so entire register is cleared */
	 UART1_C1 = 0;  /* We need all default settings, so entire register is cleared */ 

	 /* Calculate baud settings */
	 sbr = (T_UWORD)((48000*1000)/(9600 * 16 * 2));
	        
	 /* Save off the current value of the UARTx_BDH except for the SBR field */
	 temp = UART0_BDH & ~(0x1F);
	 
	    
	 UART0_BDH = temp |  UART_BDH_SBR(((sbr & 0x1F00) >> 8));
	 UART0_BDL = (char)(sbr);
	 
	 /* Save off the current value of the UARTx_BDH except for the SBR field */
	 temp = UART1_BDH & ~(0x1F);
	 
	    
	 UART1_BDH = temp |  UART_BDH_SBR(((sbr & 0x1F00) >> 8));
	 UART1_BDL = (char)(sbr);
	    
	 /* Determine if a fractional divider is needed to get closer to the baud rate */
	 //brfa = (((sysclk*32000)/(baud * 16)) - (sbr * 32));
	    
	 /* Save off the current value of the UARTx_C4 register except for the BRFA field */
	 //temp = UART0_C4 & ~(UART_C4_BRFA(0x1F));
	    
	 //UART_C4_REG(uartch) = temp |  UART_C4_BRFA(brfa);    

	 /* Enable receiver and transmitter */
	 UART0_C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK);
	 UART1_C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK);
}

/********************************************************************/
/*
 * Wait for space in the UART Tx FIFO and then send a character
 *
 * Parameters:
 *  channel      UART channel to send to
 *  ch			 character to send
 */ 
void uart0_putchar (char ch)
{
	/* Wait until space is available in the FIFO */
    while(!(UART0_S1 & UART_S1_TDRE_MASK));				//while(!(UART_S1_REG(channel) & UART_S1_TDRE_MASK));
    
    /* Send the character */
    UART0_D = (char)ch;
 }
void uart1_putchar (char ch)
{
	/* Wait until space is available in the FIFO */
    while(!(UART1_S1 & UART_S1_TDRE_MASK));
    
    /* Send the character */
    UART1_D = (char)ch;
 }
/*
 * Wait for a character to be received on the specified UART
 *
 * Parameters:
 *  channel      UART channel to read from
 *
 * Return Values:
 *  the received character
 */
char uart0_getchar ()
{
    /* Wait until character has been received */
    //while (!(UART0_S1 & UART_S1_RDRF_MASK));
    
    /* Return the 8-bit data from the receiver */
    return UART0_D;
}
char uart1_getchar ()
{
    /* Wait until character has been received */
    //while (!(UART0_S1 & UART_S1_RDRF_MASK));
    
    /* Return the 8-bit data from the receiver */
    return UART1_D;
}
/*
 * Check to see if a character has been received
 *
 * Parameters:
 *  channel      UART channel to check for a character
 *
 * Return values:
 *  0       No character received
 *  1       Character has been received
 
int uart_getchar_present ()
{
    return (UART0_S1 & UART_S1_RDRF_MASK);
}
*/
